package com.twotiger.and.adapter;

import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.others.UserNamepage;
import com.twotiger.and.activity.user.ModifyLoginPwdPage;
import com.twotiger.and.bean.User;

/**
 * 账户设置
 * @Description: 
 * @Author douxy
 * @Date 2015年8月15日 下午12:19:26
 */
public class AccountSettingAdapter extends BaseAdapter {
    protected static final int RESETSUCCES = 6;
    protected static final int RESETFIAL = 7;
    private String[] optionstr = { "姓名", "账号", "托管账号", "登录密码_null", "托管账号密码" };
    private BaseActivity context;
    private User user;
    private LayoutInflater inflater;
    private BaseHandler handler;
    private HashMap<String, String> parm;

    public AccountSettingAdapter(BaseActivity context, User user, BaseHandler handler) {
        super();
        this.context = context;
        this.user = user;
        inflater = LayoutInflater.from(context);
        this.handler = handler;
        parm = context.getParm();
    }

    @Override
    public int getCount() {
        return optionstr.length;
    }

    @Override
    public Object getItem(int position) {
        return optionstr[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int index = position;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.acount_setting_item, null);
        }
        View title_line = ViewHolder.get(convertView, R.id.title);
        View devide_line = ViewHolder.get(convertView, R.id.devide_line);
        TextView accout_list_text = ViewHolder.get(convertView, R.id.accout_list_text);
        TextView accout_list_text_value = ViewHolder.get(convertView, R.id.accout_list_text_value);
        ImageView toright_array = ViewHolder.get(convertView, R.id.toright_array);

        //初始化
        title_line.setVisibility(View.GONE);
        devide_line.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) devide_line.getLayoutParams();
        layoutParams.leftMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_16);

        toright_array.setVisibility(View.VISIBLE);
        String[] item = optionstr[index].split("_");
        accout_list_text.setText(item[0]);
        if (index == 0 || item.length == 2) {
            title_line.setVisibility(View.VISIBLE);
            layoutParams.leftMargin = 0;
        }
        switch (index) {
        case 0:
            accout_list_text_value.setText(user.name);
            break;
        case 1:
            accout_list_text_value.setText(user.phone);
            toright_array.setVisibility(View.INVISIBLE);
            break;
        case 2:
            accout_list_text_value.setText(user.thirdAccountName);
            toright_array.setVisibility(View.INVISIBLE);
            break;
        case 3:
        case 4:
            accout_list_text_value.setText("点此可重置");
            break;
        default:
            break;
        }
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                switch (index) {
                case 0:
                    intent.setClass(context, UserNamepage.class);
                    context.startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
                    break;
                case 1:
                case 2:
                    return;
                case 3:
                    intent.setClass(context, ModifyLoginPwdPage.class);
                    context.startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
                    break;
                case 4:
                    parm.clear();
                    parm.put("token", context.getToken());
                    context.getDate(parm, Constants.RESETHFPWD, handler, RESETSUCCES, RESETFIAL, true, false, true);
                    return;
                default:
                    break;
                }

            }
        });
        return convertView;
    }

}
